<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="700"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!--        创建用户-->
        <!--        {-->
        <!--        "name": "姓名",-->
        <!--        "password": "密码",-->
        <!--        "email": "邮箱",-->
        <!--        "type": 用户类型：1=本地用户 2=AD用户,-->
        <!--        "status": 用户状态：1=启用 2=禁用,-->
        <!--        }-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名称">
          <a-input v-decorator="['name', {
           rules: [
              {
                required: true,
               message: '名称不能为空！',
              },
            ],
            }]" placeholder="请输入名称" />
        </a-form-item>
<!--        {-->
<!--        "name": "名称",-->
<!--        "contact": "联系人姓名",-->
<!--        "mobile": "手机号",-->
<!--        "email": "邮箱",-->
<!--        "region": "区域",-->
<!--        "province": "省市",-->
<!--        "city": "城市",-->
<!--        "address": "详细地址",-->
<!--        "customer_code": "客户编码，用输入框就行",-->
<!--        "type": 1=零售 2=工程 3=咨询 4=服务,-->
<!--        }-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="联系人">
          <a-input v-decorator="['contact', {
           rules: [
              {
                required: true,
               message: '联系人不能为空！',
              },
            ],
            }]" placeholder="请输入联系人" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="手机号">
          <a-input v-decorator="['mobile', {
           rules: [
              {
                required: true,
               message: '手机号不能为空！',
              },
            ],
            }]" placeholder="请输入手机号" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="邮箱">
          <a-input v-decorator="['email', {
           rules: [
              {
                required: true,
               message: '邮箱不能为空！',
              },
            ],
            }]" placeholder="请输入邮箱" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="区域">
          <a-select v-decorator="['region', {
            rules: [
              {
                required: true,
                message: '区域不能为空!',
              },
            ],
            }]" placeholder="请选择区域" @change="handleChange">
            <a-select-option v-for="(i,index) in regionList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="省市">
          <a-cascader
            v-decorator="['province', {
            rules: [
              {
                required: true,
                message: '省市不能为空!',
              },
            ],
            }]"
            :options="formattedOptions"
            placeholder="请选择省/市"
            @change="handleChange"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="城市">
          <a-input v-decorator="['city', {
           rules: [
              {
                required: true,
               message: '城市不能为空！',
              },
            ],
            }]" placeholder="请输入城市" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="详细地址">
          <a-textarea v-decorator="['address', {
           rules: [
              {
                required: true,
               message: '详细地址不能为空！',
              },
            ],
            }]" placeholder="请输入详细地址" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="客户编码">
          <a-input v-decorator="['customer_code', {
           rules: [
              {
                required: true,
               message: '客户编码不能为空！',
              },
            ],
            }]" placeholder="请输入客户编码" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户类型">
          <a-select v-decorator="['type', {
            rules: [
              {
                required: true,
                message: '用户类型不能为空!',
              },
            ],
            }]" placeholder="请选择用户类型" @change="handleChange">
            <a-select-option v-for="(i,index) in typeList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction, postAction } from '@api/manage'
import moment from 'moment'
import { axios } from '@/utils/request'
//src/assets/ksh/json/pc-code.json
import pcCodeData from '@/assets/ksh/json/pc-code.json'

export default {
  name: 'membersModal',
  components: {
    pcCodeData
  },
  data() {
    return {
      pcCodeData: pcCodeData,
      formattedOptions:[],
      regionList: [
        { name: 'SOUTH', key: 'SOUTH' },
        { name: 'EAST', key: 'EAST' },
        { name: 'NORTH', key: 'NORTH' },
        { name: 'CENTRAL', key: 'CENTRAL' },
        { name: 'EAST2', key: 'EAST2' },
        { name: 'WEST', key: 'WEST' },
      ],
      typeList: [
        { name: '零售', key: '1' },
        { name: '工程', key: '2' },
        { name: '咨询', key: '3' },
        { name: '服务', key: '4' },
      ],
      confirmLoading: false,
      title: '',
      options:[],
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '200px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // "statusList": 用户状态：1=启用 2=禁用,
      statusList: [
        { name: '启用', key: '1' },
        { name: '禁用', key: '2' },
      ],
      url: {
        // add: '/cxm/cmsArticle/add',
        // edit: '/cxm/cmsArticle/edit'
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    getCurrentData() {
      if (this.currentTime === '') {
        this.currentTime = moment().format('YYYY-MM-DD')
      }
    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'name',
          'contact',
          'mobile',
          'email',
          'region',
          'province',
          'city',
          'address',
          'customer_code',
          'type',
        ))
      })
    },
    handleChange(value) {
      console.log(value)
    },
    fetchData() {
      const jsonData = this.pcCodeData

      this.formattedOptions = jsonData.map((province) => ({
        value: province.code,
        label: province.name,
        children: province.children.map((city) => ({
          value: city.code,
          label: city.name
        }))
      }));
      console.log(this.formattedOptions)

    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
          } else {
            httpurl += this.url.edit
          }
          method = 'post'
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          // httpAction(httpurl, formData, method).then(res => {
          //   if (res.success) {
          //     that.$message.success(res.message)
          //     that.$emit('refresh')
          //   } else {
          //     that.$message.error(res.message)
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false
          //   that.close()
          //   that.$parent.$parent.loadData()
          // })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>